package com.emapp.lccode.activity

import android.app.DatePickerDialog.OnDateSetListener
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.DatePicker
import androidx.lifecycle.Observer
import com.emapp.lccode.R
import com.emapp.lccode.adapter.ProjectAdapter
import com.emapp.lccode.adapter.ProjectGridAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.Project
import com.emapp.lccode.databinding.ActivityProjectList2Binding
import com.emapp.lccode.databinding.ActivityProjectListBinding
import com.emapp.lccode.mode.ProjectViewModel
import com.emapp.lccode.utils.DividerOrientation
import com.emapp.lccode.utils.divider
import com.emapp.lccode.utils.getColorExt
import com.emapp.lccode.utils.grid
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.vertical
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.Toaster
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.util.ActivityMessenger.finish
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**项目
 */
class ProjectGridListActivity : BaseActivity1<ProjectViewModel, ActivityProjectList2Binding>(),
    OnDateSetListener {
    var datas: ArrayList<Project> = arrayListOf()
    private val projectAdapter: ProjectGridAdapter by lazy { ProjectGridAdapter() }
    private var keywords = ""
    var page = 1
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        mViewBind.lvTitle.tvTitle.text = "项目"
        mViewBind.lvTitle.tvRight.text = ""
        EventBus.getDefault().register(this)
        mViewBind.rvList.run {
            grid(2)
            adapter = projectAdapter
            divider {
                orientation = DividerOrientation.HORIZONTAL
                includeVisible = true
                setDivider(10, true)
                setColor(getColorExt(R.color.background))
            }
        }
        projectAdapter.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
                val intent =
                    Intent(this@ProjectGridListActivity, ProjectDeviceListActivity::class.java)
                intent.putExtra("project_id", item.id)
                intent.putExtra("project_name", item.sort_name)
                startActivity(intent)
            }
            projectAdapter.submitList(datas)
            //isEmptyViewEnable = true
            // emptyView = layoutInflater.inflate(R.layout.listitem_empty, mViewBind.rvList, false)
        }
        mViewBind.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(this@ProjectGridListActivity))
            setRefreshFooter(ClassicsFooter(this@ProjectGridListActivity))
            setEnableLoadMore(false)
            setOnRefreshListener {
                page = 1
                getList()
            }
            setOnLoadMoreListener {
                page++
                getList()
            }
        }
        showLoading()
        getList()
        mViewModel.getDeviceAllType("0")
        mViewBind.tvSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                keywords = v.text.toString().trim()
                showLoading()
                page = 1
                getList()
                true
            } else {
                false
            }
        }
    }

    private fun getList() {
        mViewModel.getUnFinishuProjectList(keywords, page);
    }

    override fun createObserver() {
        mViewModel.run {
            projectList.observe(this@ProjectGridListActivity, Observer { resultState ->
                parseState(resultState, {
                    if (page == 1) {
                        datas.clear()
                        if (it != null) {
                            datas.addAll(it)
                            if (it.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishRefresh()
                            }
                        } else {
                            mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                        }
                    } else {
                        if (it != null) {
                            datas.addAll(it)
                            if (it.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishLoadMore()
                            }
                        } else {
                            mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                        }
                    }
                    //projectAdapter.submitList(datas)
                    projectAdapter.notifyDataSetChanged()
                }, {
                    Toaster.show(it.errorMsg)
                    mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                })
                if (datas.isEmpty()) {
                    mViewBind.emptyview.lvBg.visibility = View.VISIBLE
                } else {
                    mViewBind.emptyview.lvBg.visibility = View.GONE
                }
            })
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.btSearch,
            mViewBind.lvTitle.tvRight
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

                R.id.tv_right -> {
                }

                R.id.bt_search -> {
                    keywords = mViewBind.tvSearch.text.toString().trim()
                    showLoading()
                    page = 1
                    getList()
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }

            else -> {}
        }
    }


    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
    }

}
